//
//  ViewController.swift
//  CPT3
//
//  Created by onemade on 12/7/15.
//  Copyright © 2015 CNPC. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let fileName = "cnpc.png"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func createEidtableCopyOfDatabaseIfNeeded(){
        let fileManager = NSFileManager.defaultManager()
        let writableDBPath = applicationDocumentsDirectoryFile()
        let dbExist = fileManager.fileExistsAtPath(writableDBPath)
        
        if !dbExist {
            let defaultDBPath = NSBundle.mainBundle().resourcePath?.stringByAppendingString(fileName)
            do {
                try fileManager.copyItemAtPath(defaultDBPath!, toPath: writableDBPath)
            }catch let e as NSError{
                print(e.debugDescription)
            }
            
        }
    }
    
    func applicationDocumentsDirectoryFile() -> String {
        let documentsDirectorys = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let documentDirectory = documentsDirectorys[0]
        let path = documentDirectory.stringByAppendingString(fileName)
        return path
    }
    
}

